import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2023-06-25
 * Time: 23:23
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public int count;
    public void dfs(TreeNode root, int max) {
        if (root == null) {
            return;
        }
        // 直接比较即可, 不用什么单调栈什么的
        // 因为 max 是形参, 回溯是 max 还是当前路径上的最大值
        if (root.val >= max) {
            count++;
            max = root.val;
        }
        dfs(root.left, max);
        dfs(root.right, max);
    }

    public int goodNodes(TreeNode root) {
        dfs(root, root.val);
        return count;
    }
}
